package com.vitabuybuy.tv.ui.mine.setting

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivitySettingBinding
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.library.common.ext.setOnclickNoRepeat
import com.vitabuybuy.tv.ui.viewmodel.NoViewModel
import com.library.common.toast.XToast
import com.library.common.util.PictureUtils
import com.vitabuybuy.tv.ui.WebViewActivity
import com.vitabuybuy.tv.ui.mine.PersonalInfoActivity
import com.vitabuybuy.tv.utils.DataCleanManager
import com.vitabuybuy.tv.utils.EventCode
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_setting.*
import org.greenrobot.eventbus.EventBus


class SettingActivity : BaseActivity<NoViewModel, ActivitySettingBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_setting

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.setting))
        tvCacha.text = DataCleanManager.getTotalCacheSize(mContext)
    }

    override fun eventHandling() {
        setOnclickNoRepeat(rlPersonalInfo,rlLanguageSelection,rlHelpCenter
               , rlClearCache,rlAboutUs,rlContactCustomerService,rlFeedBack,btnLogout){v->
            when (v) {
                rlPersonalInfo -> {
                    PersonalInfoActivity.openActivity(
                        mContext
                    )
                }
                rlLanguageSelection -> {
                    LanguageSelectionActivity.openActivity(mContext)
                }
                rlClearCache -> {
                    PictureUtils.clearCache(mContext)
                    DataCleanManager.clearAllCache(mContext)
                    tvCacha.text = DataCleanManager.getTotalCacheSize(mContext)
                    XToast.success("清理成功")
                }
                rlAboutUs -> {
                    WebViewActivity.openActivity(mContext, "", "", 4)
                }
                rlHelpCenter -> {
                    HelpCenterActivity.openActivity(mContext)
                }
                rlContactCustomerService -> {
                    viewModel.getServicePhone()
                }
                rlFeedBack -> {
                    FeedbackActivity.openActivity(mContext)
                }
                btnLogout -> {
                    AlertDialog.Builder(this)
                        .setTitle(getString(R.string.logout))
                        .setMessage(getString(R.string.are_you_sure_login_out))
                        .setCancelable(true)
                        .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                            EventBus.getDefault()
                                .post(MessageEvent(EventCode.login_out, getString(R.string.logout)))
                        }
                        .setNegativeButton(getString(R.string.cancel)) { _, _ -> }.create().show()
                }
            }
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                AndPermission.with(this)
                    .runtime()
                    .permission(Permission.CALL_PHONE)
                    .onDenied {
                        if (AndPermission.hasAlwaysDeniedPermission(this, it)) {
                            // 这些权限被用户总是拒绝。
                            XToast.normal(getString(R.string.request_necessary_permissions))
                        }
                    }
                    .onGranted {
                        val intent = Intent(Intent.ACTION_DIAL)
                        val data = Uri.parse("tel:${msg.obj as String}")
                        intent.data = data
                        startActivity(intent)
                    }.start()
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, SettingActivity::class.java)
        }
    }
}
